<template>
  <a-modal
    :open="open"
    width="640px"
    :title="rowData?.zydh ? '修改移动单' : '新增移动单'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <!-- <a-form-item label="类别" class="w-1/2">
        <a-radio-group v-model:value="formState.djlb">
          <a-radio :value="0">药材</a-radio>
          <a-radio :value="1">物资</a-radio>
        </a-radio-group>
      </a-form-item> -->
      <a-form-item label="制单人:" class="w-full">
        {{ empName }}
      </a-form-item>
      <a-form-item label="出库仓库" class="w-1/2" v-bind="validateInfos.ckckbh">
        <SelectWarehouse v-model:value="formState.ckckbh" @select="onSelectSupplier" />
      </a-form-item>
      <a-form-item label="入库仓库" class="w-1/2" v-bind="validateInfos.rkckbh">
        <SelectWarehouse v-model:value="formState.rkckbh" @select="onSelectSupplier" />
      </a-form-item>
      <!-- <a-form-item label="移动日期" class="w-80">
        <a-date-picker v-model:value="formState.jhrq" class="w-full" placeholder="移动日期" :allow-clear="false" value-format="YYYY-MM-DD HH:mm:ss" />
      </a-form-item> -->
      <a-form-item label="出库方式" class="w-full">
        <a-radio-group v-model:value="formState.ckfs">
          <a-radio :value="0">直接出库</a-radio>
          <a-radio :value="1">申请单出库</a-radio>
          <a-radio :value="2">调拨单出库</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.cmemo" :rows="3" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import SelectWarehouse from '@/components/SelectWarehouse'
import { post } from '@/utils/request'
import useUserStore from '@/store/user'

const { empName } = useUserStore()

const router = useRouter()
const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  // djlb: 0,
  ckckbh: '',
  rkckbh: '',
  ckfs: '',
  cmemo: '',
  ckckmc: '',
  rkckmc: ''
})
const rulesState = ref({
  ckckbh: [{ required: true, message: '请选择出库仓库' }],
  rkckbh: [{ required: true, message: '请选择入库仓库' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
watchEffect(() => {
  if (props.open) {
    formState.value = { ckfs: 0, ...props.rowData }
  }
})
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()
  if (formState.value?.ckckbh && formState.value?.rkckbh) {
    const { code, data } = await post('/materials/psiform/doSaveTransferFormHead', formState.value)
    if (code === 0) {
      message.success('保存成功')
      if (!props.rowData?.ckckbh) {
        router.push({
          path: '/material/warehouse/move-detail',
          query: {
            zydh: data.zydh
          }
        })
      }
      onCancel()
      emit('ok')
    }
  } else {
    message.error('请填写完整信息')
  }
}
const onSelectSupplier = (_, supplier) => {
  formState.value.gysbh = supplier?.gysbh
  formState.value.gysmc = supplier?.gysmc
}
</script>
<style lang="less" scoped></style>
